package com.mvetc.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mvetc.dao.MovieDao;
import com.mvetc.dao.impl.MovieDaoImpl;
import com.mvetc.entity.Movie;
import com.mvetc.entity.PageInfo;
import com.mvetc.service.MovieService;

public class MovieServiceImpl implements MovieService {

	private MovieDao movieDao = new MovieDaoImpl();
	
	@Override
	public PageInfo<Movie> getBy(Map<String, String> map) {
		PageInfo<Movie> findBy = movieDao.findBy(map);
		return findBy;
	}

	@Override
	public int add(Movie movie) {
		int insert = movieDao.insert(movie);
		return insert;
	}

	@Override
	public int deleteById(String movieId) {
		int deleteById = movieDao.deleteById(movieId);
		return deleteById;
	}

	@Override
	public int modify(Movie movie) {
		int updateMovie = movieDao.updateMovie(movie);
		return updateMovie;
	}

	@Override
	public List<Map<String, Object>> getMoviesInfo(String...where) {
		String[] showColumnsName = {"movieId","movieName","moviePicture","movieLink","movieActors","movieCount","movieRating","moviePublishDate","movieIntro"};
		String[] selectColumnsName = {"movie_id","movie_name","movie_picture","movie_link","movie_actors","movie_count","movie_rating","movie_publish_date","movie_intro"};
		String[] tableNames = {"movie_info"};
		String[] joinColumnsName = {};
		List<Map<String, Object>> findMoviesInfo = movieDao.findMoviesInfo(showColumnsName, selectColumnsName, tableNames, joinColumnsName, where);
		return findMoviesInfo;
	}
	
	@Override
	public List<Map<String, Object>> getCommentsByMovieId(String movieId) {
		String[] showColumnsName = {"commentContent","commentDate","userName","userPicture"};
		String[] selectColumnsName = {"comment_content","comment_date","user_name","user_picture"};
		String[] tableNames = {"user_info","comment_info"};
		String[] joinColumnsName = {"user_id"};
		String where = "movie_id="+movieId;
		
		List<Map<String, Object>> findMoviesCommentInfo = movieDao.findMoviesInfo(showColumnsName, selectColumnsName, tableNames, joinColumnsName, where);
		return findMoviesCommentInfo;
	}

	@Override
	public List<Map<String, Object>> getUserInfoByName(String userName) {
		String[] showColumnsName = {"userName","userId","userPicture","memberStatus","userPassword","userContact","userIntro"};
		String[] selectColumnsName = {"user_name","user_id","user_picture","member_status","user_password","user_contact","user_intro"};
		String[] tableNames = {"user_info"};
		String[] joinColumnsName = {};
		String where = "user_name='"+userName+"'";
		List<Map<String, Object>> findUserInfo = movieDao.findMoviesInfo(showColumnsName, selectColumnsName, tableNames, joinColumnsName, where);
		return findUserInfo;
	}

	@Override
	public int addComment(String commentContent, String datetime, String userId, String movieId) {
		String tableName = "comment_info";
		String[] columns = {"comment_content","comment_date","user_id","movie_id"};
		String[] values = {commentContent,datetime,userId,movieId};
		int addComment = movieDao.insertInfo(tableName, columns, values);
		return addComment;
	}

	@Override
	public int updateInfo(Map<String, String> set, Map<String, String> where) {
		String tableName = "user_info";
		int updateInfo = movieDao.updateInfo(tableName, set, where);
		return updateInfo;
	}

	@Override
	public int deleteComment(String commentContent, String datetime, String userId, String movieId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Map<String, Object>> getCollect(String userId, String collectMovieId) {
		String[] showColumnsName = {"userId","collectMovieId"};
		String[] selectColumnsName = {"user_id","collect_movie_id"};
		String[] tableNames = {"user_movie_collect"};
		String[] joinColumnsName = {};
		String where = "user_id='"+userId+"' and collect_movie_id='"+collectMovieId+"'";
		List<Map<String, Object>> findCollectInfo = movieDao.findMoviesInfo(showColumnsName, selectColumnsName, tableNames, joinColumnsName, where);
		return findCollectInfo;
	}
	
	@Override
	public int addCollect(String userId, String collectMovieId) {
		String tableName = "user_movie_collect";
		String[] columns = {"user_id","collect_movie_id"};
		String[] values = {userId,collectMovieId};
		int insertInfo = movieDao.insertInfo(tableName, columns, values);
		return insertInfo;
	}

	@Override
	public int deleteCollect(String userId, String collectMovieId) {
		String tableName = "user_movie_collect";
		Map<String, String> where = new HashMap<>(16);
		where.put("user_id", userId);
		where.put("collect_movie_id", collectMovieId);
		int deleteInfo = movieDao.deleteInfo(tableName, where);
		return deleteInfo;
	}
	
	
}
